package com.czq.controllor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czq.common.BaseContext;
import com.czq.common.Result;
import com.czq.dao.OrderDetailDao;
import com.czq.pojo.*;
import com.czq.service.AddressBookService;
import com.czq.service.OrderDetailService;
import com.czq.service.OrdersService;
import com.czq.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderControllor {
@Autowired
private UserService userService;

@Autowired
private OrdersService ordersService;
@Autowired
private OrderDetailService orderDetailService;
@Autowired
private AddressBookService addressBookService;

//下单
    @PostMapping("/submit")
    public Result buy(@RequestBody Orders orders){
ordersService.submit(orders);
return Result.success("下单成功");

    }
    //查看订单
    @GetMapping("/userPage")
    public Result selectall(@RequestParam Integer page, @RequestParam Integer pageSize){
        Page<Orders>  p=new Page(page, pageSize);

        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> o=new LambdaQueryWrapper<>();
        o.eq(Orders::getUserId,userid);
        o.orderByDesc(Orders::getOrderTime);
        ordersService.page(p,o);//查询出orders的分页

        Page<OrdersDto> pp=new Page(page,pageSize);
        List<OrdersDto> selectall = ordersService.selectall();

        BeanUtils.copyProperties(p,pp,"records");
        pp.setRecords(selectall);
        return  Result.success(pp);




    /*    LambdaQueryWrapper<OrderDetail> ordersLambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();

        Long userid = BaseContext.getCurrentId();
        queryWrapper.eq(Orders::getUserId,userid);
        List<Orders> list = ordersService.list(queryWrapper);//查询用户所有的订单id，根据订单id来查orderdetail表

        List<Long> orderesid=new ArrayList<>();
        for (Orders orders : list){
            Long orderid = orders.getId();
            orderesid.add(orderid);
        }
      select * from OrderDetail where  id  in(1,2,3 (orderesid  ));
        ordersLambdaQueryWrapper.in(OrderDetail::getOrderId,orderesid);
        orderDetailService.page(p,ordersLambdaQueryWrapper);
        return  Result.success(p);*/


    }
    @GetMapping("/page")
    public  Result select(@RequestParam Integer page, @RequestParam Integer pageSize, Long number,String beginTime, String endTime) throws ParseException {

        Page<Orders> p = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> q = new LambdaQueryWrapper<>();
        q.orderByDesc(Orders::getOrderTime);
        q.like(number != null, Orders::getNumber, number);

        SimpleDateFormat util = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (beginTime != null && !"".equals(beginTime.trim())) {
            Date begin = util.parse(beginTime);
            q.ge(Orders::getOrderTime, begin);
        }

        if (endTime != null && !"".equals(endTime.trim())) {
            Date end = util.parse(endTime);
            q.le(Orders::getOrderTime, end);
        }

        ordersService.page(p, q);
        return Result.success(p);

    }
}
